<?php
    /**
     *  Copyright (c) 2013, dsphinx@plug.gr
     *  All rights reserved.
     *
     *  Redistribution and use in source and binary forms, with or without
     *  modification, are permitted provided that the following conditions are met:
     *   1. Redistributions of source code must retain the above copyright
     *      notice, this list of conditions and the following disclaimer.
     *   2. Redistributions in binary form must reproduce the above copyright
     *      notice, this list of conditions and the following disclaimer in the
     *      documentation and/or other materials provided with the distribution.
     *   3. All advertising materials mentioning features or use of this software
     *      must display the following acknowledgement:
     *      This product includes software developed by the dsphinx.
     *   4. Neither the name of the dsphinx nor the
     *      names of its contributors may be used to endorse or promote products
     *     derived from this software without specific prior written permission.
     *
     *  THIS SOFTWARE IS PROVIDED BY dsphinx ''AS IS'' AND ANY
     *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     *  DISCLAIMED. IN NO EVENT SHALL dsphinx BE LIABLE FOR ANY
     *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     *
     */

    DEFINE ("__AJAX", TRUE);

    require_once ('../Library/Ajax/Ajax.php');

    $_return = NULL;

    if ($results = file_get_contents("php://input")) {

        // $results = json_decode($results, TRUE);
        $results = $_POST; // POST faster for simple calls

        $category = Ajax_Call::IN($results['category']);

       // $_return = " Κατηγορια απο ΒΔ  = " . $category;

//tsalik
        include('../../Private/admin_advert/auth_db.php');
        $sql_f1 = 'SELECT field_search.id as fcai, field_search.field_id as fcafi, category_action.id as cai, field_search.category_action_id as fcacai, field_search.ordering as fcao, field.name as fn, field.id as fi, category.name as cn, field.parameters as fpara FROM field_search, category_action, field, category WHERE field_search.field_id = field.id AND field_search.category_action_id=category_action.id AND category_action.category_id=category.id AND category.id='.$category.' ORDER BY field_search.ordering';
        $result_f1 = mysqli_query($con, $sql_f1);
        $result_f = mysqli_query($con, $sql_f1);
        //echo "Fields<br>";
        //echo $sql_f1;
      //  $form = '<h4>έξτρα επιλογές</h4>';
        //echo '<table>';
        $row_f = mysqli_fetch_array($result_f);

 //       $form = $form . '<form id="form_advert_insert" method="post" enctype="multipart/form-data" action="?page=admin_advert/advert_data_ins">';
        $form = $form . '<input class="input-small" name="fcai" type="hidden" value="'.$row_f['fcai'].'"/>';
        while($row_f1 = mysqli_fetch_array($result_f1))
        {

            if ($row_f1['fpara'] == "TEXT")
            {
                $form = $form . '<span class="help-inline">'.$row_f1['fn'].'</span>';
                $form = $form . '<input class="input-small" type="text" name="var'.$row_f1['fi'].'" value="" />';
            }
            else if ($row_f1['fpara'] == "MEDIA")
            {
                $form = $form . '<span class="help-inline">'.$row_f1['fn'].'</span>';
                $form = $form . '<input class="input-small" type="file" name="var'.$row_f1['fi'].'" value="" />';
            }
            else if ($row_f1['fpara'] == "PASSWORD")
            {
                $form = $form . '<span class="help-inline">'.$row_f1['fn'].'</span>';
                $form = $form . '<input class="input-small" type="password" name="var'.$row_f1['fi'].'" value="" />';
            }
            else if ($row_f1['fpara'] == "TEXTAREA")
            {
                $form = $form . '<span class="help-inline">'.$row_f1['fn'].'</span>';
                $form = $form . '<textarea COLS="40" ROWS="6" name="var'.$row_f1['fi'].'">  </textarea>';
            }
            else if ($row_f1['fpara'] == "RADIOBOX")
            {
                $sql_f2 = 'SELECT field_matrix.id as fmi,field_matrix.field_id as fmfi, field_matrix.parent_id as fmpi, field_matrix.ordering as fmo,field.id,field.name FROM field_matrix, field WHERE field_matrix.field_id=field.id AND field_matrix.parent_id ='.intval($row_f1['fi']).' ORDER BY field_matrix.ordering';
                $result_f2 = mysqli_query($con, $sql_f2);
                //echo $sql_f2;
                $form = $form . '<span class="help-inline">'.$row_f1['fn'].'</span>';
                while($row_f2 = mysqli_fetch_array($result_f2))
                {
                    $form = $form . '<input type="radio" name="var'.$row_f1['fi'].'" value="'.$row_f2['fmfi'].'" />'.$row_f2['name'];
                }
            }
            else if ($row_f1['fpara'] == "CHECKBOX")
            {
                $sql_f2 = 'SELECT field_matrix.id as fmi,field_matrix.field_id as fmfi, field_matrix.parent_id as fmpi, field_matrix.ordering as fmo,field.id,field.name FROM field_matrix, field WHERE field_matrix.field_id=field.id AND field_matrix.parent_id ='.intval($row_f1['fi']).' ORDER BY field_matrix.ordering';
                $result_f2 = mysqli_query($con, $sql_f2);
                //echo $sql_f2;
                $form = $form . '<span class="help-inline">'.$row_f1['fn'].'</span>';
                while($row_f2 = mysqli_fetch_array($result_f2))
                {
                    $form = $form . '<input type="checkbox" name="var'.$row_f1['fi'].'" value="'.$row_f2['fmfi'].'" />'.$row_f2['name'];
                }
            }
            else if ($row_f1['fpara'] == "DROPDOWNBOX")
            {
                $sql_f2 = 'SELECT field_matrix.id as fmi,field_matrix.field_id as fmfi, field_matrix.parent_id as fmpi, field_matrix.ordering as fmo,field.id,field.name FROM field_matrix, field WHERE field_matrix.field_id=field.id AND field_matrix.parent_id ='.intval($row_f1['fi']).' ORDER BY field_matrix.ordering';
                $result_f2 = mysqli_query($con, $sql_f2);
                //echo $sql_f2;
                $form = $form . '<span class="help-inline">'.$row_f1['fn'].'</span>';
                $form = $form . '<select class="chzn-select-deselect chzn-done" id= "var'.$row_f1['fi'].'" name="var'.$row_f1['fi'].'">';
                $form = $form . '<option selected value="">Επιλέξτε</option>';
                while($row_f2 = mysqli_fetch_array($result_f2))
                {


                    $form = $form . '<option value="'.$row_f2['fmfi'].'">'.$row_f2['name'].'</option>';
                    //$form = $form . '<input type="checkbox" name="val'.$row_f1['fi'].'" value="'.$row_f2['fmfi'].'" />';
                }
                $form = $form.'</select>';
            }

            //else{
            //echo '<tr><td>'.$row_f1['fcafi'].'</td><td>'.$row_f1['cai'].'</td><td>'.$row_f1['fcacai'].'</td><td>'.$row_f1['fcao'].'</td><td>'.$row_f1['fn'].'</td><td>'.$row_f1['cn'].'</td><td>'.$row_f1['fpara'].'</td></tr>';
            //}//<td>[ <a href="?page=admin_advert/field_matrix_edit&id_ad=2&id='.$row['bid'].'">Επεξεργασία</a> ]</td><td>[ <a href="?page=admin_advert/field_matrix_edit&id_ad=3&id='.$row['bid'].'">Διαγραφή</a> ]</td></tr>';
        }
        $sql_f2 = 'SELECT category_action.category_id, category_action.action_group_id as caagi, action.id, action.name, action.action_group_id, action.ordering FROM category_action, action WHERE category_action.action_group_id=action.action_group_id AND category_action.category_id='.$category.' ORDER BY action.ordering';
        $result_f2 = mysqli_query($con, $sql_f2);
        // echo $sql_f2;
        $form = $form . '<span class="help-inline">Ενέργεια</span>';
        $form = $form . '<select class="chzn-select-deselect chzn-done" id="action_id" name="action_id">';
        $form = $form . '<option selected value="">Επιλέξτε</option>';
        while($row_f2 = mysqli_fetch_array($result_f2))
        {


            $form = $form . '<option value="'.$row_f2['id'].'">'.$row_f2['name'].'</option>';
            //$form = $form . '<input type="checkbox" name="val'.$row_f1['fi'].'" value="'.$row_f2['fmfi'].'" />';
        }
        $form = $form.'</select>';
        $_return .= '<div class="control-group info">
              <div class="controls">'.$form.' </div>
         </div>';
 //       $form = $form . '<input name="click" type="hidden" value="1"/>';
 //       $form = $form . '<input type="submit" value="Προσθήκη αγγελίας"/>';
 //       $form = $form . '</form>';
 //       echo $form;

//tsalik
/*
        if ($category == 1) {

            $_return .= '<br/>
        <div class="control-group warning">
              <div class="controls">
                    <span class="help-inline">Τιμή </span>        <input name="asd" class="input-small" placeholder="1002" type="text">
                    <span class="help-inline">Έτος </span>        <input name="23423" class="input-small" placeholder="2013" type="text">
                    <span class="help-inline">Εμβαδον </span>        <input name="ss" class="input-small" placeholder="τμ" type="text">
             </div>
         </div>';

        } else {

            $_return .= '<br/>
        <div class="control-group info">
              <div class="controls">
                    <span class="help-inline">Κατρανισ </span>        <input name="inputWarnssing" class="input-small" placeholder="11" type="text">
             </div>
         </div>';


        }
*/

    }




    echo  $_return ;

?>

